package leetcode.code0654;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {

	public TreeNode constructMaximumBinaryTree(int[] nums) {
		return f(nums, 0, nums.length - 1);
	}

	private TreeNode f(int[] nums, int L, int R) {
		if (L > R) {
			return null;
		}
		if (L == R) {
			return new TreeNode(nums[L]);
		}
		int max = -1;
		int r = 0;
		for (int i = L; i <= R; i++) {
			int num = nums[i];
			if (num > max) {
				max = num;
				r = i;
			}
		}
		TreeNode root = new TreeNode(max);
		root.left = this.f(nums, L, r - 1);
		root.right = this.f(nums, r + 1, R);
		return root;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
